#!/bin/sh
#__参数初始化__
CURL_TIMEOUT=20
Name="139xxxx1234"
Password="b0axxxxea2c153d1ab72f732e"
LOGIN_DATA="{\"userName\":\"${Name}\",\"userPassword\":\"${Password}\"}"
USER_DATA="{\"userName\":\"${Name}\"}"
SUCCESS="提速成功"
FAIL="提速失败"
EXPIRE="服务到期，请续费"
NUMBER=1	

#__push+ (可选)__
#__网址http://www.pushplus.plus/__
#__PUSHKEY="2ccfce1cxxxxx1d29"__

#__推送函数__
pushFun(){
        
    if [ -n ${COOLKEY} ]
    then
        curl -k -s -H "Content-Type: application/json" -X POST -d $1 "https://push.xuthus.cc/send/${COOLKEY}"
    fi

    if [ -n ${PUSHKEY} ]
    then
        #__消息转换为URL编码__
        MESSAGE=`echo $1 | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g'`
        curl "http://www.pushplus.plus/send/${PUSHKEY}?content=${MESSAGE}"
    fi
}

#__json解析__
parse_json(){
echo "${1//\"/}" | sed "s/.*$2:\([^,}]*\).*/\1/"
}


#__登陆__
logger -t "【光速联提速脚本】" "————————登陆中————————"
loginInfo=`curl -k -s -H "Content-Type: application/json" -X POST -d ${LOGIN_DATA} "https://www.fangyb.com:2039/biz/user/login.do"`
loginCode=$(parse_json ${loginInfo} "code")
Auth=$(parse_json ${loginInfo} "data")
if [ 0 == ${loginCode} ]
then
    logger -t "【光速联提速脚本】" "————————登陆成功————————"
elif [ 12 == ${loginCode} ]
then
    logger -t "【光速联提速脚本】" "————————用户名或密码错误————————"
    exit 0
elif [ 11 == ${loginCode} ]
then
    logger -t "【光速联提速脚本】" "————————未注册，请检查手机号是否正确或前往注册后再次运行————————"
    exit 0
else
    logger -t "【光速联提速脚本】" "————————登录失败，请重试————————"
    exit 0
fi

#__不是每次运行都能成功，所以多次执行__


#__当前提速状态__
myOrderInfo=`curl -k -s -H "Content-Type: application/json" -H "Authorization: ${Auth}" -X POST -d ${USER_DATA} "https://www.fangyb.com:2039/biz/common/myOrder.action"`
stateCode=`echo "${myOrderInfo}" | awk -F  '"' '{print $(NF-1)}'`
className=`echo "${myOrderInfo}" | awk -F  '"' '{print $38}'`
orderId=`echo "${myOrderInfo}" | awk -F  '"' '{print $82}'`
validDate=`echo "${myOrderInfo}" | awk -F  '"' '{print $66}'`

#__判断购买是否到期__
today=$(date "+%Y-%m-%d")
t1=`date -d "${validDate}" +%s`
t2=`date -d "${today}" +%s`
if [ ${t2} -gt ${t1} ]
then
	logger -t "【光速联提速脚本】" "————————购买已到期，请续费————————"
    pushFun ${EXPIRE}
#    break
fi

#__构建提速参数__
SPEED_DATA="{\"userName\":\"${Name}\",\"className\":\"${className}\",\"orderId\":\"${orderId}\"}"

sleep 1
if [ "true" == ${stateCode} ]
then
    logger -t "【光速联提速脚本】" "————————提速状态：提速中————————"
    logger -t "【光速联提速脚本】" "————————正在重新开始提速————————"	
	#__关闭提速__
    curl -k -s -o /dev/null -H "Content-Type: application/json" -H "Authorization: ${Auth}" -X POST -d ${SPEED_DATA} "https://www.fangyb.com:2039/biz/common/closeSpeed.action"	
    sleep 2	

    logger -t "【光速联提速脚本】" "————————提速状态：未提速————————"
    logger -t "【光速联提速脚本】" "————————正在开始提速————————"
fi




while true
do
	#__开始提速__
    curl -k -s -o /dev/null -m ${CURL_TIMEOUT} -H "Content-Type: application/json" -H "Authorization: ${Auth}" -X POST -d ${SPEED_DATA} "https://www.fangyb.com:2039/biz/common/openSpeed.action"
    sleep 20
    #__提速结果检验__
    #__访问两次是为了刷新__
    curl -k -s -o /dev/null -H "Content-Type: application/json" -H "Authorization: ${Auth}" -X POST -d ${USER_DATA} "https://www.fangyb.com:2039/biz/common/speedQuery.do"
    sleep 1	
    speedQuery=`curl -k -s -H "Content-Type: application/json" -H "Authorization: ${Auth}" -X POST -d ${USER_DATA} "https://www.fangyb.com:2039/biz/common/speedQuery.do"`
    resultCode=`echo "${speedQuery}" | awk -F  '"' '{print $(NF-1)}'`
    if [ "true" == ${resultCode} ]
    then
        logger -t "【光速联提速脚本】" "————————第${NUMBER}次提速成功————————"
        echo "`echo "${speedQuery}" | awk -F  '"' '{print $(NF-1)}'`"
        pushFun ${SUCCESS}
        exit
	else
		logger -t "【光速联提速脚本】" "————————第${NUMBER}次提速失败，开始重试————————"
		let "NUMBER++"
        #重试次数超过3次，则退出
        if [ ${NUMBER} > 3 ]
        then
            logger -t "【光速联提速脚本】" "————————第3次提速失败，开始重试————————"
            pushFun ${FAIL}
            break
        fi
	fi
done


#__上面3次提速完后，不死心再来一次__
#__开始提速__
curl -k -s -o /dev/null -m ${CURL_TIMEOUT} -H "Content-Type: application/json" -H "Authorization: ${Auth}" -X POST -d ${SPEED_DATA} "https://www.fangyb.com:2039/biz/common/openSpeed.action"
sleep 4

#__提速结果__
#__访问两次是为了刷新__
curl -k -s -o /dev/null -H "Content-Type: application/json" -H "Authorization: ${Auth}" -X POST -d ${USER_DATA} "https://www.fangyb.com:2039/biz/common/speedQuery.do"
sleep 1
speedQuery=`curl -k -s -H "Content-Type: application/json" -H "Authorization: ${Auth}" -X POST -d ${USER_DATA} "https://www.fangyb.com:2039/biz/common/speedQuery.do"`
resultCode=`echo "${speedQuery}" | awk -F  '"' '{print $(NF-1)}'`
if [ "true" == ${resultCode} ]
then
	logger -t "【光速联提速脚本】" "————————第4次提速成功————————"
	echo "`echo "${speedQuery}" | awk -F  ',' '{for (i=6;i<=10;i++){print $i}}'`"
	pushFun ${SUCCESS}
	exit
else
	logger -t "【光速联提速脚本】" "————————第4次提速失败，不再重试————————"	
fi
